package com.aikon.wht.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author haitao.wang
 */
public class MongoDBConfigService {

    private static Logger logger = LoggerFactory.getLogger(MongoDBConfigService.class);

    private String host;

    private String databaseName;

    private String collectionName;

    private int connectionsPerHost;

    private int threadsAllowedToBlockForConnectionMultiplier;

    private int maxWaitTime;

    public MongoDBConfigService() {

    }

    public MongoDBConfigService(String host, String databaseName, String collectionName,  int connectionsPerHost, int threadsAllowedToBlockForConnectionMultiplier, int maxWaitTime) {
        this.databaseName = databaseName;
        this.collectionName = collectionName;
        this.host = host;
        this.connectionsPerHost = connectionsPerHost;
        this.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
        this.maxWaitTime = maxWaitTime;
    }

    public MongoClientOptions  getMongoClientOptions() {
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(connectionsPerHost);
        builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);
        builder.maxWaitTime(maxWaitTime);
        return builder.build();
    }

    public MongoClient getMongoClient() {
        return new MongoClient(host, this.getMongoClientOptions());
    }

    public MongoDatabase getDb(String databaseName) {
        MongoClient mongoClient = this.getMongoClient();
        return mongoClient.getDatabase(databaseName);
    }

    public MongoCollection getCollection() {
        return this.getDb(databaseName).getCollection(collectionName);
    }

    public void init() {
        logger.info("This is init method.");
    }

    public void destroy() {
        logger.info("This is destroy method.");
    }

}

